const app = getApp()
var QQMapWX = require('../../../libs/qqmap-wx-jssdk.js');
import qqmap from '../../../utils/map.js';
// 实例化API核心类
var qqmapsdk = new QQMapWX({
    key: 'GTDBZ-WFSRX-JOT4W-7WYBD-Z2CTO-7QBEM' // 必填
});
 Page({
  data: {
    toitem:'',
    keys:['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','P','Q','S','T','U','W','X','Y','Z'],
    citylist: [],
    //下面是热门城市数据，模拟数据
    newcity: ['北京', '上海', '广州', '深圳', '重庆', '天津'],
  },
  onShow: function() {
    //this.getLocate()
    var _this = this;
    //调用获取城市列表接口
    qqmapsdk.getCityList({
      success: function(res) {//成功后的回调
        _this.sortCity(res.result[1])
      },
      fail: function(error) {
        //console.error(error);
      },
      complete: function(res) {
        //console.log(res);
      }
    });
},
sortCity:function(params){
    //var params = res.data.data;
    var cityList = []
    params.splice(0,31);
    params.splice(71,16);
    //        城市列表
    // [{index:A,list:[{cityId:"",cityName:""},{cityId:"",cityName:""},{cityId:"",cityName:""}]}] []*/

    for (var i = 0; i < params.length; i++) {
      var letterFirst = params[i].pinyin[0].substr(0,1).toUpperCase();
      if (isCityList(letterFirst)) {
        for (var k = 0; k < cityList.length; k++) {
          if (letterFirst == cityList[k].id){
            cityList[k].data.push({
              id: params[i].id,
              cityName: params[i].name,
              //latitude: params[i].location.lat,
              //longitude: params[i].location.lng,
            });
            break;
          }
        }
      }else{
        cityList.push({
            id: letterFirst,
            data: [{
              id: params[i].id,
              cityName: params[i].name,
              //latitude: params[i].location.lat,
              //longitude: params[i].location.lng,
            }]
          })
      }
    }
    //判断当前标识是否在cityList当中
    function isCityList(letterFirst) {
      var bStop = false;
      for (var i = 0; i < cityList.length; i++){
        if (cityList[i].id == letterFirst){
          bStop = true;
          break;
        }
      }
      return bStop; //存在为true 不存在未false
    }
    //     //排序
    cityList.sort((item1, item2) => {
      if (item1.id > item2.id) {
        return 1;
      } else {
        return -1;
      }
    })
    //console.log(cityList)
    this.setData({
      citylist:cityList
    })
  },
  jumpTo: function (e) {
    let option = e.currentTarget.dataset.item;
    this.setData({
      toitem: option
    })
  } ,
  cityTap:function(e){
    let city = e.currentTarget.dataset.val;
    var session = wx.getStorageSync('session')
    session.city = city
    wx.setStorageSync('session',session);
    wx.navigateTo({
        url:'/pages/login/personal'
    })
  },
  getLocate:function(){
    let that=this;
    new qqmap().getLocateInfo().then(function (val) {//这个方法在另一个文件里，下面有贴出代码
      if (val.indexOf('市') !== -1) {//这里是去掉“市”这个字
        //console.log(val.indexOf('市') - 1);
        val = val.slice(0, val.indexOf('市'));
        //console.log(val);
      }
      that.setData({ 
        locateCity: val
      });
      //把获取的定位和获取的时间放到本地存储
      wx.setStorageSync('locatecity', { city: val, time: new Date().getTime() });
    });
  },
    onShareAppMessage: function() {
      return {
          title: "天意美业直通车",
          path: "/pages/index/index?puid="+wx.getStorageSync('uid')
      };
    },
})